﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Client_Test
{
    class Program
    {
        //static bool closeSocket;

        //static private void StreamRead(NetworkStream stream)
        //{
        //    Byte[] bytes = new Byte[256];
        //    string data;
        //    int i;
        //    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
        //    {
        //        // Translate data bytes to a ASCII string.
        //        data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
        //        Console.WriteLine("Received: {0}", data);
        //    }
        //    closeSocket = true;
        //}
        
        //static private void StreamWrite(NetworkStream stream)
        //{
        //    string message = Console.ReadLine();
        //    while (message != "exit")
        //    {
        //        Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

        //        // Send the message to the connected TcpServer. 
        //        stream.Write(data, 0, data.Length);

        //        //Console.WriteLine("Sent: {0}", message);
        //        message = Console.ReadLine();
        //    }
        //    closeSocket = true;
        //}

        static void Main(string[] args)
        {
            //closeSocket = false;
            //try
            //{
            //    // Create a TcpClient. 
            //    // Note, for this client to work you need to have a TcpServer  
            //    // connected to the same address as specified by the server, port 
            //    // combination.
            //    Int32 port = 13000;
            //    Console.WriteLine("Enter IP Address: ");
            //    //IPAddress addr = IPAddress.Parse(Console.ReadLine());

            //    TcpClient client = new TcpClient(Console.ReadLine(), port);
            //    NetworkStream stream = client.GetStream();

            //    Thread readThread = new Thread(() => StreamRead(stream));
            //    Thread writeThread = new Thread(() => StreamWrite(stream));
            //    readThread.Start();
            //    writeThread.Start();
            //    while (!closeSocket) { }
            //    if (readThread.IsAlive) { readThread.Abort(); }
            //    if (writeThread.IsAlive) { writeThread.Abort(); }
            //    stream.Close();
            //    client.Close();
            //}
            //catch (ArgumentNullException e)
            //{
            //    Console.WriteLine("ArgumentNullException: {0}", e);
            //}
            //catch (SocketException e)
            //{
            //    Console.WriteLine("SocketException: {0}", e);
            //}
            int port = 13000;
            Console.WriteLine("Enter IP Address: ");
            TcpClient socket = new TcpClient(Console.ReadLine(), port);

            GameState.client = new ClientThread(ref socket);
            GameState.client.StartListening();
            Console.WriteLine("My ID: " + GameState.client.ID);
            NetworkManager.SendMapMessage(5, 5, GameState.client.ID);
            NetworkManager.SendMapMessage(100, 78, GameState.client.ID);
            //byte[] array = new byte[sizeof(int) * 3];
            //int[] nums = new int[3];
            //nums[0] = 1;
            //nums[1] = 2888;
            //nums[2] = 75;
            //Buffer.BlockCopy(nums, 0, array, 0, array.Length);
            //GameState.client.Send(array);
            //GameState.client.Send(array);

            string message = Console.ReadLine();
            NetworkManager.SendChatMessage(message, GameState.client.ID);
            //int[] nums2 = new int[2];
            //nums2[0] = 2;
            //nums2[1] = message.Length;
            //Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
            //byte[] array2 = new byte[sizeof(int) * 2];
            //Buffer.BlockCopy(nums2, 0, array2, 0, array2.Length);
            //GameState.client.Send(array2);
            //GameState.client.Send(data);
            //while (message != "exit")
            //{
            //    Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

            //    // Send the message to the connected TcpServer. 
            //    client.Send(data);

            //    //Console.WriteLine("Sent: {0}", message);
            //    message = Console.ReadLine();
            //}

            Console.WriteLine("\n Press Enter to continue...");
            Console.Read();
        }
    }
}
